import { useIntl } from "react-intl";
import { App as AntdApp } from "antd";
import { LibAppView, Service } from "../../../../lib";
import { useLocation } from "react-router-dom";
import { useRef } from "react";

export const AdminApiHandler = ({ children }: any) => {
    const app = AntdApp.useApp();
    const intl = useIntl();
    const appView = LibAppView.useApp();
    const locaton = useLocation();
     
    return (<Service.ApiHandleProvider handler={[(data, response, opt) => {
        const { sendOptions,extra, isError } = opt;
        const respData = data || response;
        if(!/^200$/.test(respData.statusCode) || isError) {
            if(["Login_Expire"].includes(respData.statusCode)) {
                const loginState = sessionStorage.getItem("loginState");
                if(loginState && loginState?.length > 0) {
                    if(loginState !== "LoginExpired"){
                        sessionStorage.setItem("loginState", "LoginExpired");
                        app.modal.error({
                            title: intl.formatMessage({ id: "admin.layout.modal.question", defaultMessage: "提示"}),
                            content: intl.formatMessage({ id: "admin.login.status.expired", defaultMessage: "登录已过期，请重新登录！"}),
                            okText: intl.formatMessage({ id: "admin.layout.modal.ok", defaultMessage: "确定"}),
                            cancelText: intl.formatMessage({ id: "admin.layout.modal.cancel", defaultMessage: "取消"}),
                            cancelButtonProps: {
                                hidden: true
                            },
                            
                            onOk: () => {
                                appView.goto("home.login", {
                                    replace: true,
                                    state: {
                                        from: location.href
                                    }
                                });
                            }
                        });
                    }
                    opt.preventDefault = true;
                    return true;
                } else {
                    appView.goto("home.login", {
                        replace: true,
                        state: {
                            from: location.href
                        }
                    });
                    opt.preventDefault = true;
                }
            } else if(/^Refused$/.test(respData.statusCode)) {
                const exceptionData =  {
                    title: respData.statusCode,
                    subTitle: intl.formatMessage({
                        id: "admin.access.noright"
                    }),
                    backPageId: "home.login",
                    status: "403"
                };
                extra?.hide();
                if(locaton.pathname.startsWith("/admin") && sendOptions.endPoint !== "login.status") {
                    appView.goto("admin.404", {
                        state: exceptionData
                    });
                } else {
                    appView.goto("home.erorrPage", {
                        state: exceptionData
                    });
                }
                return true;
            }
        }
        return data;
    }]}>{children}</Service.ApiHandleProvider>);
};